package com.sx.pipeline.ui.needs.operator.detail

import android.Manifest
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.permissionx.guolindev.PermissionX
import com.sx.mvvmlibs.base.EasyAdapter
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ReleaseImageAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.app.config.OperatorNeedsType
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.ui.needs.operator.NeedsVM
import com.sx.pipeline.utils.PermissionHelper


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 运营商 需求 详情
 */
@Route(path = RoutePath.Mine.OPERATOR_NEEDS_DETAIL)
class OperatorNeedsDetailActivity :
    CYDataBindingBaseActivity<com.sx.pipeline.databinding.ActOperatorNeedsDetailBinding, NeedsVM>(R.layout.act_operator_needs_detail),
    View.OnClickListener {
    @JvmField
    @Autowired(name = IntentKey.ID)
    var needsId: Int? = 0

    private var mLiveMachineImageAdapter: ReleaseImageAdapter? = null//现场机器图片适配器
    private var mExplorationImageAdapter: ReleaseImageAdapter? = null//现场照片图片适配器
    private var mDrawingsImageAdapter: ReleaseImageAdapter? = null//施工图纸图片适配器


    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }

        needsId?.let { mViewModel.getNeedOrderDetail(it) }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.needOrderDetailEvent) {
            //显示 取消需求 / 删除需求
            when (it?.status) {
                OperatorNeedsType.PUBLISHED.code -> {
                    mBinding.tvStatus.text = getString(R.string.txt_published)
                }
                OperatorNeedsType.CANCELLED.code -> {
                    mBinding.tvStatus.text = getString(R.string.txt_cancelled)
                }

            }

            //0:设备查找 1:单位合作、2:服务
            when (it?.needDownType) {
                0 -> {
                    //设备名称
                    mBinding.tvDeviceName.text = it.omsNeedEquipment?.name
                    //车牌号码
                    mBinding.tvProductNumber.text = it.omsNeedEquipment?.equipmentNo
                    //查找方式
                    mBinding.sbSearchMethod.rightText = "发布需求信息"
                    if (!TextUtils.isEmpty(it?.omsNeedEquipment?.equipmentImage)) {
                        //现场机器照片
                        it?.omsNeedEquipment?.equipmentImage?.split(",")
                            ?.let { it1 ->
                                initLiveMathineImg(it1)
                                mBinding.tabDevicePhoto.visibility = View.VISIBLE
                            }
                    }

                    mBinding.tabDeviceFind.visibility = View.VISIBLE
                    mBinding.sbSearchMethod.visibility = View.VISIBLE
                }
                1 -> {
                    //工作量
                    mBinding.tvWorkload.text = it?.omsNeedUnit?.quantity
                    //计量单位
                    mBinding.sbMeasurementUnit.rightText = it?.omsNeedUnit?.unit
                    //开始时间
                    mBinding.sbStartTime.rightText = it.omsNeedUnit?.startDate
                    //结束时间
                    mBinding.sbEndTime.rightText = it.omsNeedUnit?.endDate
                    //信息推送范围
                    when (it.omsNeedUnit?.pushRange) {
                        0 -> mBinding.sbPushRange.rightText = "省"
                        1 -> mBinding.sbPushRange.rightText = "市"
                        2 -> mBinding.sbPushRange.rightText = "县"
                    }
                    if (!TextUtils.isEmpty(it?.omsNeedUnit?.siteImage)) {
                        //施工现场照片
                        it?.omsNeedUnit?.siteImage?.split(",")
                            ?.let { it1 ->
                                initExplorationImg(it1)
                                mBinding.tabExplorationPhotos.visibility = View.VISIBLE
                            }
                    }
                    if (!TextUtils.isEmpty(it?.omsNeedUnit?.constructionImage)) {
                        //施工图纸
                        it?.omsNeedUnit?.constructionImage?.split(",")
                            ?.let { it1 ->
                                initDrawingsImg(it1)
                                mBinding.tabConstructionDrawings.visibility = View.VISIBLE
                            }
                    }


                    mBinding.tabUnitFind.visibility = View.VISIBLE
                    mBinding.sbPushRange.visibility = View.VISIBLE
                    mBinding.sbStartTime.visibility = View.VISIBLE
                    mBinding.sbEndTime.visibility = View.VISIBLE

                }
            }
            //发起单位
            mBinding.sbUnitName.rightText = it?.unitName
            //标题
            mBinding.sbTitle.rightText = it?.title
            //需求描述
            mBinding.tvDesc.setText(it?.description)
            //需求类型
            mBinding.sbRequirementType.rightText = it?.needType
            //施工地点
            mBinding.sbArea.rightText = it?.addressDetail
            mBinding.tvAddressDetail.text = it?.address
            //联系人
            mBinding.sbContact.rightText = it?.contact
            //联系电话
            mBinding.tvContactPhone.text = it?.contactPhone
            //需求发布人
            mBinding.sbCreateby.rightText = it?.createBy
            //发布时间
            mBinding.sbCreateTime.rightText = it?.createTime
        }

    }

    //现场机器
    fun initLiveMathineImg(data: List<String>) {
        mLiveMachineImageAdapter = ReleaseImageAdapter(this, data)
        mBinding.rvLiveMachine.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvLiveMachine.adapter = mLiveMachineImageAdapter
        mLiveMachineImageAdapter?.selectMode = EasyAdapter.SelectMode.SINGLE_SELECT
    }

    //现场照片
    fun initExplorationImg(data: List<String>) {
        mExplorationImageAdapter = ReleaseImageAdapter(this, data)
        mBinding.rvExplorationPhotos.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvExplorationPhotos.adapter = mExplorationImageAdapter
        mExplorationImageAdapter?.selectMode = EasyAdapter.SelectMode.SINGLE_SELECT
    }

    //施工图纸
    fun initDrawingsImg(data: List<String>) {
        mDrawingsImageAdapter = ReleaseImageAdapter(this, data)
        mBinding.rvConstructionDrawings.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvConstructionDrawings.adapter = mDrawingsImageAdapter
        mDrawingsImageAdapter?.selectMode = EasyAdapter.SelectMode.SINGLE_SELECT
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.sb_area -> {
                // 选择施工地点
                // 申请权限
                PermissionHelper.requestTip(this!!,"选择施工地点需要授权定位权限",object :
                    PermissionUtils.SimpleCallback{
                    override fun onGranted() {
                        RouteManager.Main.intentToSearchLocation()

                    }

                    override fun onDenied() {
                        ToastUtils.showShort("请授权后再继续操作")
                    }
                },Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS)

//                PermissionX.init(this)
//                    .permissions(
//                        Manifest.permission.ACCESS_FINE_LOCATION,
//                        Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
//                    )
//                    .onExplainRequestReason { scope, deniedList ->
//                        scope.showRequestReasonDialog(
//                            deniedList,
//                            "核心基础功能基于这些权限,请允许,否则影响app正常使用",
//                            "确认",
//                            "取消"
//                        )
//                    }
//                    .onForwardToSettings { scope, deniedList ->
//                        scope.showForwardToSettingsDialog(
//                            deniedList,
//                            "您需要手动在“设置”中允许必要的权限",
//                            "确定",
//                            "取消"
//                        )
//                    }
//                    .request { allGranted, grantedList, deniedList ->
//                        if (allGranted) {
//                            RouteManager.Main.intentToSearchLocation()
//                        } else {
//                            Toast.makeText(
//                                this,
//                                "这些权限被拒绝,可能影响管建宝正常使用" + ": $deniedList",
//                                Toast.LENGTH_LONG
//                            ).show()
//                        }
//                    }
            }
        }
    }
}